package com.liukaixin.product.navi;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liukaixin on 16/10/12.
 */

public class GetCarFragment extends LazyLoadFragment {

    private Handler handler = new Handler();

    private View line1;
    private View line2;
    private View line19;
    private View line22;
    private View line3;
    private View line4;
    private View line36;
    private View line38;
    private View line42;
    private View line53;
    private View line55;
    private View line58;
    private View line60;
    private View line104;
    private View line107;
    private View line110;
    private View line111;
    private View line121;
    private View line123;
    private View line33;
    private View line30;
    private View line27;
    private View line24;
    private View line21;
    private View line35;
    private View line50;
    private View line47;
    private View line45;
    private View line41;
    private View line52;
    private View line67;
    private View line64;
    private View line61;
    private View line44;
    private View line6;
    private View line5;
    private View line103;
    private View line102;
    private View line100;
    private View line98;
    private View line95;
    private View line92;
    private View line89;
    private View line120;
    private View line119;
    private View line116;
    private View line113;
    private View line134;
    private View line132;
    private View line130;
    private View line127;
    private View line124;

    private Button finishGetCarBtn;

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    private String exit = "A";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static GetCarFragment newInstance(int sectionNumber) {
        GetCarFragment fragment = new GetCarFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public GetCarFragment() {

    }

    @Override
    public void initData() {
        final String carPosition = AppController.getInstance().getCache().getAsString("emptyPosition");
        if (carPosition == null || carPosition.isEmpty()){
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("请选择入口");
            builder.setMessage("未检测到您的车,请先停车");
            builder.setPositiveButton("去停车", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent toMap = new Intent(getActivity(),MapActivity.class);
                    startActivity(toMap);
                    getActivity().finish();
                }
            });
            builder.setNegativeButton("取消", null);
            builder.setIcon(R.drawable.logo);
            builder.show();
        }else {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("您的车停在[" + carPosition + "]号车位, 请选择出口");
            builder.setSingleChoiceItems(new String[]{"A", "B", "C", "D", "E", "F"}, 0,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case 0:
                                    exit = "A";
                                    break;
                                case 1:
                                    exit = "B";
                                    break;
                                case 2:
                                    exit = "C";
                                    break;
                                case 3:
                                    exit = "D";
                                    break;
                                case 4:
                                    exit = "E";
                                    break;
                                case 5:
                                    exit = "F";
                                    break;
                            }
                        }
                    });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getActivity(), "请稍后, 生成路径中……", Toast.LENGTH_SHORT).show();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            List<View> pathList = getPath(exit, carPosition);
                            showPath(pathList);
                        }
                    }, 3000);
                }
            });
            builder.setIcon(R.mipmap.ic_launcher);
            builder.show();
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_get_car, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews(view);
        ((AppCompatActivity) getActivity()).getSupportActionBar();
        setHasOptionsMenu(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_finish_park) {
            Toast.makeText(getActivity(), "sa", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initViews(View view) {
        finishGetCarBtn = (Button) view.findViewById(R.id.finish_get_car_btn);
        finishGetCarBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppController.getInstance().getCache().clear();
                Intent toHome = new Intent(getActivity(), MainActivity.class);
                startActivity(toHome);
                getActivity().finish();
            }
        });
        line1 = view.findViewById(R.id.line1);
        line2 = view.findViewById(R.id.line2);
        line19 = view.findViewById(R.id.line19);
        line22 = view.findViewById(R.id.line22);
        line3 = view.findViewById(R.id.line3);
        line4 = view.findViewById(R.id.line4);
        line36 = view.findViewById(R.id.line36);
        line38 = view.findViewById(R.id.line38);
        line42 = view.findViewById(R.id.line42);
        line53 = view.findViewById(R.id.line53);
        line55 = view.findViewById(R.id.line55);
        line58 = view.findViewById(R.id.line58);
        line60 = view.findViewById(R.id.line60);
        line104 = view.findViewById(R.id.line104);
        line107 = view.findViewById(R.id.line107);
        line110 = view.findViewById(R.id.line110);
        line111 = view.findViewById(R.id.line111);
        line121 = view.findViewById(R.id.line121);
        line123 = view.findViewById(R.id.line123);
        line33 = view.findViewById(R.id.line33);
        line30 = view.findViewById(R.id.line30);
        line27 = view.findViewById(R.id.line27);
        line24 = view.findViewById(R.id.line24);
        line21 = view.findViewById(R.id.line21);
        line35 = view.findViewById(R.id.line35);
        line50 = view.findViewById(R.id.line50);
        line47 = view.findViewById(R.id.line47);
        line45 = view.findViewById(R.id.line45);
        line41 = view.findViewById(R.id.line41);
        line52 = view.findViewById(R.id.line52);
        line67 = view.findViewById(R.id.line67);
        line64 = view.findViewById(R.id.line64);
        line61 = view.findViewById(R.id.line61);
        line44 = view.findViewById(R.id.line44);
        line6 = view.findViewById(R.id.line6);
        line5 = view.findViewById(R.id.line5);
        line103 = view.findViewById(R.id.line103);
        line102 = view.findViewById(R.id.line102);
        line100 = view.findViewById(R.id.line100);
        line98 = view.findViewById(R.id.line98);
        line95 = view.findViewById(R.id.line95);
        line92 = view.findViewById(R.id.line92);
        line89 = view.findViewById(R.id.line89);
        line120 = view.findViewById(R.id.line120);
        line119 = view.findViewById(R.id.line119);
        line116 = view.findViewById(R.id.line116);
        line113 = view.findViewById(R.id.line113);
        line134 = view.findViewById(R.id.line134);
        line132 = view.findViewById(R.id.line132);
        line130 = view.findViewById(R.id.line130);
        line127 = view.findViewById(R.id.line127);
        line124 = view.findViewById(R.id.line124);
    }

    private List<View> getPath(String exit, String emptyPosition) {
        if (exit.equals("A") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (exit.equals("A") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (exit.equals("A") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line4);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (exit.equals("A") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (exit.equals("A") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line1);
            list.add(line2);
            list.add(line3);
            list.add(line4);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (exit.equals("B") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line33);
            list.add(line30);
            list.add(line27);
            list.add(line24);
            list.add(line21);
            list.add(line22);
            return list;
        }
        if (exit.equals("B") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line50);
            list.add(line47);
            list.add(line45);
            list.add(line41);
            list.add(line42);
            return list;
        }
        if (exit.equals("B") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line52);
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line60);
            return list;
        }
        if (exit.equals("B") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line38);
            list.add(line36);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (exit.equals("B") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line35);
            list.add(line52);
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line58);
            list.add(line55);
            list.add(line53);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (exit.equals("C") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line35);
            list.add(line33);
            list.add(line30);
            list.add(line27);
            list.add(line24);
            list.add(line21);
            list.add(line22);
            return list;
        }
        if (exit.equals("C") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line42);
            return list;
        }
        if (exit.equals("C") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line60);
            return list;
        }
        if (exit.equals("C") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line52);
            list.add(line50);
            list.add(line47);
            list.add(line44);
            list.add(line41);
            list.add(line38);
            list.add(line36);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (exit.equals("C") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line67);
            list.add(line64);
            list.add(line61);
            list.add(line58);
            list.add(line55);
            list.add(line53);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (exit.equals("D") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line3);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (exit.equals("D") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (exit.equals("D") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (exit.equals("D") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line4);
            list.add(line104);
            list.add(line107);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (exit.equals("D") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line6);
            list.add(line5);
            list.add(line121);
            list.add(line123);
            return list;
        }
        if (exit.equals("E") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line102);
            list.add(line100);
            list.add(line98);
            list.add(line95);
            list.add(line92);
            list.add(line89);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (exit.equals("E") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line120);
            list.add(line119);
            list.add(line116);
            list.add(line113);
            list.add(line110);
            list.add(line107);
            list.add(line104);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (exit.equals("E") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line121);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (exit.equals("E") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line120);
            list.add(line119);
            list.add(line113);
            list.add(line110);
            list.add(line111);
            return list;
        }
        if (exit.equals("E") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line123);
            return list;
        }
        if (exit.equals("F") && emptyPosition.equals("3-1")) {
            List<View> list = new ArrayList<>();
            list.add(line102);
            list.add(line100);
            list.add(line98);
            list.add(line95);
            list.add(line92);
            list.add(line89);
            list.add(line19);
            list.add(line22);
            return list;
        }
        if (exit.equals("F") && emptyPosition.equals("5-2")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line119);
            list.add(line113);
            list.add(line110);
            list.add(line107);
            list.add(line104);
            list.add(line36);
            list.add(line38);
            list.add(line42);
            return list;
        }
        if (exit.equals("F") && emptyPosition.equals("6-3")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line120);
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line121);
            list.add(line53);
            list.add(line55);
            list.add(line58);
            list.add(line60);
            return list;
        }
        if (exit.equals("F") && emptyPosition.equals("12-3")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line119);
            list.add(line116);
            list.add(line113);
            list.add(line111);
            return list;
        }
        if (exit.equals("F") && emptyPosition.equals("15-1")) {
            List<View> list = new ArrayList<>();
            list.add(line103);
            list.add(line120);
            list.add(line134);
            list.add(line132);
            list.add(line130);
            list.add(line127);
            list.add(line124);
            list.add(line123);
            return list;
        }
        return null;
    }

    private void showPath(List<View> pathList) {
        for (View view : pathList) {
            setVisible(view);
        }
    }

    private void setVisible(View view) {
        view.setVisibility(View.VISIBLE);
    }

}
